package com.happyfamily.springboot.crowdsourcingplatform.controller.guo;

import com.happyfamily.springboot.crowdsourcingplatform.annotation.OperationLogDetail;
import com.happyfamily.springboot.crowdsourcingplatform.base.result.PageTableRequest;
import com.happyfamily.springboot.crowdsourcingplatform.base.result.Results;
import com.happyfamily.springboot.crowdsourcingplatform.dto.UserDto;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationType;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationUnit;
import com.happyfamily.springboot.crowdsourcingplatform.model.Need;
import com.happyfamily.springboot.crowdsourcingplatform.model.SysUser;
import com.happyfamily.springboot.crowdsourcingplatform.service.guo.EmployerAndServiceService;
import com.happyfamily.springboot.crowdsourcingplatform.service.guo.FrontNeedService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;


@Controller
@Slf4j
public class EmployerAndServiceController
{
	@Autowired
	private EmployerAndServiceService employerAndServiceService;

	@Autowired
	private FrontNeedService frontNeedService;

	@GetMapping("/api-back/employer/list")
	@ResponseBody
	@ApiOperation(value = "后端显示雇主列表", notes = "后端显示雇主列表")
	@OperationLogDetail(detail = "后端显示雇主列表", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public Results<SysUser> getAllEmployersPage(PageTableRequest request)
	{
		request.countOffset();
		Results<SysUser> rs= employerAndServiceService.getAllEmployers(request.getOffset(), request.getLimit());
		for (int i = 0; i <rs.getDatas().size() ; i++)
		{
			rs.getDatas().get(i).setTips1(employerAndServiceService.getEmployersStates(Long.valueOf(rs.getDatas().get(i).getPid())));
		}
		return rs;
	}


	@GetMapping("/api-back/employer/resetPass")
	@ResponseBody
	@ApiOperation(value = "重置密码服务", notes = "重置密码服务")
	@OperationLogDetail(detail = "重置密码服务", level = 2, operationUnit = OperationUnit.USER, operationType = OperationType.UPDATE)
	public String resetPassword(Long uid){
		return employerAndServiceService.resetPassword(uid);
	}


	@GetMapping("/api-back/employer/changeS")
	@ResponseBody
	@ApiOperation(value = "更改用户状态", notes = "更改用户状态")
	@OperationLogDetail(detail = "更改用户状态", level = 2, operationUnit = OperationUnit.USER, operationType = OperationType.UPDATE)
	public String changeStates(Long uid,Long pid){
		return employerAndServiceService.changeStates(uid,pid);
	}

	@GetMapping("/api-back/employer/specifyList")
	@ResponseBody
	@ApiOperation(value = "后端雇主模糊查询", notes = "后端雇主模糊查询")
	@OperationLogDetail(detail = "后端雇主模糊查询", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public Results<SysUser> getSpecifyEmployersPage(String uname,PageTableRequest request)
	{
		request.countOffset();
		Results<SysUser> rs=employerAndServiceService.getSpecifyEmployers(uname,request.getOffset(), request.getLimit());
		for (int i = 0; i <rs.getDatas().size() ; i++)
		{
			rs.getDatas().get(i).setTips1(employerAndServiceService.getEmployersStates(Long.valueOf(rs.getDatas().get(i).getPid())));
		}
		return rs;
	}

	@GetMapping("/api-back/employer/serviceProvidersList")
	@ResponseBody
	@ApiOperation(value = "后端服务商查询", notes = "后端服务商查询")
	@OperationLogDetail(detail = "后端服务商查询", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public Results<SysUser> getServiceProvidersList(PageTableRequest request)
	{
		request.countOffset();

		Results<SysUser> rs= employerAndServiceService.getServiceProvidersList(request.getOffset(), request.getLimit());
		for (int i = 0; i <rs.getDatas().size() ; i++)
		{
			rs.getDatas().get(i).setTips1(employerAndServiceService.getEmployersStates(Long.valueOf(rs.getDatas().get(i).getPid())));
			rs.getDatas().get(i).setTips2(employerAndServiceService.getServiceProvidersStates(Integer.valueOf(rs.getDatas().get(i).getRid())));
		}
		return rs;
	}

	@PreAuthorize("hasAuthority('back:service:update')")
	@GetMapping("/api-back/employer/changeServiceType")
	@ResponseBody
	@ApiOperation(value = "更改服务商类型", notes = "更改服务商类型")
	@OperationLogDetail(detail = "更改服务商类型", level = 2, operationUnit = OperationUnit.USER, operationType = OperationType.UPDATE)
	public String changeServiceType(Integer uid,Integer ak){
		return employerAndServiceService.changeServiceType(uid,ak);
	}

	@GetMapping("/api-back/employer/SpecifiedServiceProvidersList")
	@ResponseBody
	@ApiOperation(value = "后端服务商模糊查询", notes = "后端服务商模糊查询")
	@OperationLogDetail(detail = "后端服务商模糊查询", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public Results<SysUser> getSpecifiedServiceProvidersList(String uname,PageTableRequest request)
	{
		request.countOffset();
		Results<SysUser> rs=employerAndServiceService.getSpecifiedServiceProvidersList(uname,request.getOffset(), request.getLimit());
		for (int i = 0; i <rs.getDatas().size() ; i++)
		{
			rs.getDatas().get(i).setTips1(employerAndServiceService.getEmployersStates(Long.valueOf(rs.getDatas().get(i).getPid())));
			rs.getDatas().get(i).setTips2(employerAndServiceService.getServiceProvidersStates(Integer.valueOf(rs.getDatas().get(i).getRid())));
		}
		return rs;
	}


	@GetMapping("/api-front/employer/changePass")
	@ResponseBody
	@ApiOperation(value = "个人中心修改密码", notes = "个人中心修改密码")
	@OperationLogDetail(detail = "个人中心修改密码", level = 2, operationUnit = OperationUnit.USER, operationType = OperationType.UPDATE)
	public String chnagePassword(String pass,String account,String newPass){
		return employerAndServiceService.chnagePassword(newPass,account,pass);
     }

    @GetMapping("/api-front/employer/myPass")
    public String myPassword(HttpServletRequest request,Model model){
	    SecurityContextImpl securityContext= (SecurityContextImpl) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
	    String account=securityContext.getAuthentication().getName();
	    model.addAttribute("account",account);
	    return "frontPage/change-password";
    }

	@GetMapping("/api-front/employer/getConsultantList")
	@ResponseBody
	@ApiOperation(value = "前端获取顾问列表", notes = "前端获取顾问列表")
	@OperationLogDetail(detail = "前端获取顾问列表", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public Results<SysUser> getConsultantList(PageTableRequest request)
	{
		request.countOffset();
		Results<SysUser> rs= employerAndServiceService.getConsultantList(6,7,request.getOffset(), request.getLimit());
		for (int i = 0; i <rs.getDatas().size() ; i++)
		{
			rs.getDatas().get(i).setTips1(employerAndServiceService.getEmployersStates(Long.valueOf(rs.getDatas().get(i).getPid())));
			rs.getDatas().get(i).setTips2(employerAndServiceService.getServiceProvidersStates(Integer.valueOf(rs.getDatas().get(i).getRid())));
		}
		return rs;
	}

	@GetMapping("/api-front/employer/getConsultantListByUname")
	@ResponseBody
	@ApiOperation(value = "前端模糊查询雇主列表", notes = "前端模糊查询雇主列表")
	@OperationLogDetail(detail = "前端模糊查询雇主列表", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public Results<SysUser> getConsultantListByUname(PageTableRequest requests, String uname, String sid)
	{

		requests.countOffset();
		Results<SysUser> rs=employerAndServiceService.getConsultantListByUname(6,7,uname, sid, requests.getOffset(), requests.getLimit());
		for (int i = 0; i <rs.getDatas().size() ; i++)
		{
			rs.getDatas().get(i).setTips1(employerAndServiceService.getEmployersStates(Long.valueOf(rs.getDatas().get(i).getPid())));
			rs.getDatas().get(i).setTips2(employerAndServiceService.getServiceProvidersStates(Integer.valueOf(rs.getDatas().get(i).getRid())));
		}
		return rs;
	}

	@GetMapping("/api-front/employer/getFrontServiceList")
	@ResponseBody
	@ApiOperation(value = "前端获取服务商列表", notes = "前端获取服务商列表")
	@OperationLogDetail(detail = "前端获取服务商列表", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public Results<SysUser> getFrontServiceList(PageTableRequest request)
	{
		request.countOffset();

		Results<SysUser> rs= employerAndServiceService.getConsultantList(2,7,request.getOffset(), request.getLimit());
		for (int i = 0; i <rs.getDatas().size() ; i++)
		{
			rs.getDatas().get(i).setTips1(employerAndServiceService.getEmployersStates(Long.valueOf(rs.getDatas().get(i).getPid())));
			rs.getDatas().get(i).setTips2(employerAndServiceService.getServiceProvidersStates(Integer.valueOf(rs.getDatas().get(i).getRid())));
		}
		return rs;
	}

	@GetMapping("/api-front/employer/getFrontServiceListByUname")
	@ResponseBody
	@ApiOperation(value = "前端模糊查询服务商列表", notes = "前端模糊查询服务商列表")
	@OperationLogDetail(detail = "前端模糊查询服务商列表", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public Results<SysUser> getFrontServiceListByUname(PageTableRequest requests, String uname, String sid)
	{

		requests.countOffset();
		Results<SysUser> rs=employerAndServiceService.getConsultantListByUname(2,7,uname, sid, requests.getOffset(), requests.getLimit());
		for (int i = 0; i <rs.getDatas().size() ; i++)
		{
			rs.getDatas().get(i).setTips1(employerAndServiceService.getEmployersStates(Long.valueOf(rs.getDatas().get(i).getPid())));
			rs.getDatas().get(i).setTips2(employerAndServiceService.getServiceProvidersStates(Integer.valueOf(rs.getDatas().get(i).getRid())));
		}
		return rs;
	}

	@GetMapping("/api-front/employer/getUserInfoByUaccount")
	@ResponseBody
	@ApiOperation(value = "前端获取个人资料", notes = "前端获取个人资料")
	@OperationLogDetail(detail = "前端获取个人资料", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public SysUser getUserInfoByUaccount(HttpServletRequest request){
		SecurityContextImpl securityContext = (SecurityContextImpl) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
		String account = securityContext.getAuthentication().getName();
		SysUser user = frontNeedService.getUser(account);
		return user;
	}

}
